package com.example.toolsdemo.wiremock;

import org.apache.commons.io.FileUtils;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.util.stream.Collectors;

import static com.github.tomakehurst.wiremock.client.WireMock.*;

/**
 * @author danyubin
 * java -jar wiremock-standalone-2.26.0.jar --port 8082
 */
public class MockServer {

	public static void main(String[] args) throws IOException {
		configureFor(8082);
		removeAllMappings();

		mock("/order/1", "01");
		mock("/order/2", "02");
		mock("/order/3", "03");
	}

	private static void mock(String url, String file) throws IOException {
		ClassPathResource resource = new ClassPathResource("mock/response/" + file + ".txt");
		String result = FileUtils.readLines(resource.getFile(), "utf-8")
								.stream()
								.collect(Collectors.joining("\n"));
//		//String content = StringUtils.join(FileUtils.readLines(resource.getFile(), "utf-8")., "\n");
		// 注意指定响应头编码格式，否则会出现中文乱码问题
		stubFor(get(urlPathEqualTo(url)).willReturn(aResponse().withHeader("content-type", "text/html;charset=UTF-8").withBody(result).withStatus(200)));
	}

}
